import Vue from "vue";
import Vuex from "vuex";
import * as api from "../api"
import router from "../router";
import allRoutes from "../router/allRoutes"
import dynamicRoutes from "../router/dynamicRoutes"
import recursionRoutes from "../utils/recursionRoutes";

Vue.use(Vuex);

let currentChargeProduct = localStorage.getItem("qf-currentChargeProduct") || "[]"
let userInfo = localStorage.getItem('qf-userInfo') || '{}'
export default new Vuex.Store({
  state: {
    userInfo: JSON.parse(userInfo), // 用户信息
    sideMenu: [], //侧边栏数据
    breadCrumb: [], //面包屑数据
    dialogVisible: false, // 控制visible显示
    dialogTitle: "学员增加信息", //dialog标题
    stuData: [], //学员项目表格的数据对象
    loading: true, // 控制表格动画开关
    currentRow: {},
    order_id:"",//订单id
    //订单
    orderData: [],
    // 当前充值的产品
    currentChargeProduct:JSON.parse(currentChargeProduct)
  },
  mutations: {
    // 设置订单id
    SET_ORDER_ID(state,payload){
      state.order_id = payload
    },
    //设置当前充值的产品
    SET_CURRENT_CHARGE_PRODUCT(state,payload){
      state.currentChargeProduct = [payload]
      localStorage.setItem('qf-currentChargeProduct', JSON.stringify([payload]))
    },
    //修改dialog回显数据
    SET_CURRENT_ROW(state, payload) {
      state.currentRow = {
        ...payload
      }
    },
    SET_STU_DATA(state, payload) { //设置学员项目管理表格中的数据
      state.stuData = payload
    },
    SHOW_DIALOG(state) { //显示dialog
      state.dialogVisible = true
    },
    HIDE_DIALOG(state) { //隐藏dialog
      state.dialogVisible = false
    },
    CHANGE_DIALOG_TITLE(state, payload) {
      state.dialogTitle = payload
    },
    SET_USER_INFO(state, payload) { // 设置菜单
      state.userInfo = payload
    },
    SET_MENULIST(state, payload) { // 设置用户信息
      // // state.sideMenu = payload
      // // console.log(state.sideMenu);
      // //   // 要将用户路由动态的添加到项目中
      // //   // 将用户路由添加到dynamicRoute中的"/"路径下children属性中
      // let target = dynamicRoutes.find(item => item.path === "/")
      // // console.log(target);
      // //要将用户路由动态的添加到我们的项目之中
      // // console.log(dynamicRoutes)
      // target.children = [...payload, ...target.children]
      // router.addRoutes(dynamicRoutes)
      // // 预支付页面不需要展示在侧边菜单
      // // 使用filter 返回满足条件的对象
      // target.children = target.children.filter(item => {
      //   item.path !== "pre_order"
      // })
      // state.sideMenu = target.children
      // // for(let i = 0;i<dynamicRoutes.length;i++){
      // //   const element = dynamicRoutes[i]
      // //   router.addRoute(element)
      // // }
      // // dynamicRoutes.forEach(item => {
      // //   router.addRoute(item)
      // // })

      // state.sideMenu = payload;
      // console.log(state.sideMenu)
      //将用户路由动态的添加到router的配置routes中
      //将用户路由添加到dynamicRoutes中的"/"路径那个配置中的children属性中
      let target = dynamicRoutes.find(item => item.path === "/");
      // console.log(target)

      target.children = [...payload, ...target.children];
      //要将用户路由动态的添加到我们的项目之中
      // console.log(dynamicRoutes)
      // router.addRoutes(dynamicRoutes)
      dynamicRoutes.forEach(item => {
          router.addRoute(item)
        })
      //核心方法就是router.addRoutes([])
      //预支付订单页面不需要展示在菜单栏
      //要排除不需要展示在菜单中的页面 pre_order payment
      let excludeRoutes = ["pre_order", "payment"]

      target.children = target.children.filter(item => !excludeRoutes.includes(item.path))

      state.sideMenu = target.children;

    },
    SET_BREADCRUMB(state, payload) { // 设置面包屑
      state.breadCrumb = payload
    },
    //设置订单数据
    SET_ORDER_DATA(state, payload) {
      state.orderData = payload
    },
  },
  // 获取用户菜单
  actions: {
    async GET_MENULIST({
      commit
    }) {
      let res = await api.getMenuListApi()
      let {
        menuList
      } = res.data
      // 使用recursionRoutes的方法计算用户需要的路由，提交给mutation
      let userRoutes = recursionRoutes(menuList, allRoutes)
      commit("SET_MENULIST", userRoutes)
    },
    //获取学员列表
    GET_STULIST({
      commit,
      state
    }, payload = {}) {
      let {
        page = 1, count = 10, classes = ""
      } = payload; //解构传递的参数
      api.getStuList(page, count, classes)
        .then(res => {
          commit('SET_STU_DATA', res.data.data)
          state.loading = false
        })
    },
    //获取订单数据
    GET_ORDER_DATA({
      commit
    }, payload) {
      let order_id = payload || "";
      api.getOrderApi(order_id)
        .then(res => {
          console.log(res.data)
          if (res.data.state) {
            console.log(res.data.data)
            commit("SET_ORDER_DATA", res.data.data)
          } else {
            this.$message.error("获取订单失败")
          }
        })
    }
  },
  modules: {},
});